﻿// OperatorLibraryDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "ModuleLibraryDialog.h"
#include "afxdialogex.h"

#include "ModuleTreeView.h"
#include "OperatorListView.h"

// COperatorLibraryDialog 对话框

IMPLEMENT_DYNAMIC(CModuleLibraryDialog, CDialogEx)

CModuleLibraryDialog::CModuleLibraryDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MODULE_LIBRARY, pParent)
{
	m_pFrameWnd = NULL;
}

CModuleLibraryDialog::~CModuleLibraryDialog()
{
}

void CModuleLibraryDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CModuleLibraryDialog, CDialogEx)
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_COMMAND(ID_TOOLBAR_CHECK_OPERATOR_MODULE_ADD, &CModuleLibraryDialog::OnModuleAdd)
	ON_COMMAND(ID_TOOLBAR_CHECK_OPERATOR_MODULE_REMOVE, &CModuleLibraryDialog::OnModuleRemove)
	ON_COMMAND(ID_TOOLBAR_CHECK_OPERATOR_OPERATOR_ADD, &CModuleLibraryDialog::OnOperatorAdd)
	ON_COMMAND(ID_TOOLBAR_CHECK_OPERATOR_OPERATOR_REMOVE, &CModuleLibraryDialog::OnOperatorRemove)

END_MESSAGE_MAP()

// COperatorLibraryDialog 消息处理程序


int CModuleLibraryDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialogEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CString strMyClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
		::LoadCursor(NULL, IDC_ARROW),
		(HBRUSH)::GetStockObject(WHITE_BRUSH),
		::LoadIcon(NULL, IDI_APPLICATION));

	m_pFrameWnd = new CFrameWnd();
	m_pFrameWnd->Create(strMyClass, _T(""), WS_CHILD, CRect(0, 0, 100, 100), this);
	m_pFrameWnd->MoveWindow(0, 0, 500, 400);
	m_pFrameWnd->ShowWindow(SW_SHOW);

	CRect rect;
	GetClientRect(&rect);


	CCreateContext ctxList, ctxTree;
	ctxTree.m_pNewViewClass = RUNTIME_CLASS(CModuleTreeView);
	ctxTree.m_pCurrentDoc = NULL;
	ctxTree.m_pNewDocTemplate = NULL;
	ctxTree.m_pLastView = NULL;
	ctxTree.m_pCurrentFrame = NULL;

	ctxList.m_pNewViewClass = RUNTIME_CLASS(COperatorListView);
	ctxList.m_pCurrentDoc = NULL;
	ctxList.m_pNewDocTemplate = NULL;
	ctxList.m_pLastView = NULL;
	ctxList.m_pCurrentFrame = NULL;

	//CRect rect;
	//GetClientRect(&rect);
	m_wndSplitter.CreateStatic(m_pFrameWnd, 1, 2);
	m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CModuleTreeView), CSize(rect.Width() * 1 / 4, rect.Height()), &ctxTree);
	m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(COperatorListView), CSize(rect.Width() * 3 / 4, rect.Height()), &ctxList);


	AdjustLayout();
	PopulateModuleTree();
	PopulateOperatorList();

	return 0;
}



BOOL CModuleLibraryDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_BORDER | WS_CAPTION | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_TOOLBAR_CHECK_OPERATOR_LIBRARY, 0, 0, TRUE, 0, 0, IDB_PNG_CHECK_OPERATOR_LIBRARY_24))
	{
		TRACE0("未能创建工具栏\n");
		return -1;      // 未能创建
	}

	m_wndToolBar.SetOwner(this);
	m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
	m_wndToolBar.AdjustSizeImmediate();


	AdjustLayout();
	m_pFrameWnd->ShowWindow(SW_SHOW);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CModuleLibraryDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	AdjustLayout();
}



void CModuleLibraryDialog::AdjustLayout()
{
	int margin = GetSystemMetrics(SM_CXFRAME);


	if(m_wndToolBar.GetSafeHwnd())
	{
		CRect tbRect;
		m_wndToolBar.GetWindowRect(&tbRect);
		int nHeight = tbRect.Height();

		if (m_pFrameWnd)
		{
			CRect wndRect;
			GetWindowRect(&wndRect);
			ScreenToClient(wndRect);

			int margin = abs(wndRect.left);

			wndRect.left = margin;
			wndRect.top = nHeight;
			wndRect.right -= (2 * margin);
			wndRect.bottom -= (2 * margin);
			m_pFrameWnd->MoveWindow(wndRect);
		}
	}
}

void CModuleLibraryDialog::PopulateModuleTree()
{
	CModuleTreeView* pTree = (CModuleTreeView*)m_wndSplitter.GetPane(0, 0);
	pTree->Populate();
}

void CModuleLibraryDialog::PopulateOperatorList()
{
	COperatorListView* pList = (COperatorListView*)m_wndSplitter.GetPane(0, 1);
	pList->Populate();
}

void CModuleLibraryDialog::OnModuleAdd()
{
	AfxMessageBox("OnModuleAdd");
}

void CModuleLibraryDialog::OnModuleRemove()
{
	AfxMessageBox("OnModuleRemove");
}

void CModuleLibraryDialog::OnOperatorAdd()
{
	AfxMessageBox("OnOperatorAdd");
}

void CModuleLibraryDialog::OnOperatorRemove()
{
	AfxMessageBox("OnOperatorRemove");
}
